<?php

namespace app\api\controller;

use app\common\controller\BasicApi;
use app\common\model\CourseModel;
use app\common\model\UserModel;
use think\db;

class Course extends BasicApi
{
    /**
     * 获取课程详情
     */
    public function getCourseDetail(){
        $course_id = $this->request->get('course_id');
        $course = CourseModel::where('id',$course_id)->find();
        $course['logo'] = explode('|',$course['logo']);
        $course['intro'] = explode('|',$course['intro']);
        $course['teacher'] = UserModel::where('id',$course['teacher_id'])->find();
        if(!empty($course)){
            $this->success('获取课程详情成功！',$course,200);
        }else{
            $this->error('获取课程详情失败！','',400);
        }
    }

    /**
     * 多规格
     */
    public function getGoodsSpecPrice(){
        $goods_id = $this->request->get('goods_id');
        $spec_name = $this->request->get('spec_name');
        $spec_value = $this->request->get('spec_value');
        $current_specs = json_decode($this->request->get('current_specs'),true);
        foreach ($current_specs as $key=>&$value){
            if($value['name']==$spec_name){
                $value['value']=$spec_value;
            }
        }
        $current_spec_str = implode(';;',array_column($current_specs,'value'));
        //找规格表
        $product = GoodsProductModel::where('spec',$current_spec_str)
            ->where('goods_id',$goods_id)
            ->find();
        $product['current_specs'] = $current_specs;
        $product['current_spec_str'] = explode(';;',$current_spec_str);
        if(!empty($product)) {
            $this->success('获取规格价格及库存成功！', $product, 200);
        }else{
            $this->success('获取规格价格及库存失败！','',401);
        }
    }

    /**
     * 查找已存的朋友圈图片
     */
    public function getPyqPhoto(){
        $goods_id = $this->request->get('goods_id');
        $photo = GoodsModel::where(['id'=>$goods_id])->value('photo');
        if(!empty($photo)){
            $this->success('获取朋友圈图片成功！',$photo,200);
        }else{
            $this->error('获取朋友圈图片失败！','',401);
        }
    }

    public function postPyqPhoto(){
        $goods_id = $this->request->post('goods_id');
        $photo = $this->request->post('photo');
        $update = GoodsModel::where(['id'=>$goods_id])->update(['photo'=>$photo]);
        if(!empty($update)){
            $this->success('更新朋友圈图片成功！',$update,200);
        }else{
            $this->error('更新朋友圈图片失败！','',401);
        }
    }

    /**
     * 获取商品所有购买记录
     */
    public function getBuyList(){
        $goods_id = $this->request->get('goods_id');
        if(empty($goods_id)) $this->error('商品id不能为空！','',400);
        //找到订单
        $order_ids = OrderGoodsModel::where('goods_id',$goods_id)->field('group_concat(order_id) as order_ids')->find()['order_ids'];
        $order = OrderModel::where('id','in',$order_ids)
            ->order('id desc')
            ->field('user_id,id,create_time')
            ->select();
        foreach ($order as $key=>&$value){
            $user = UserModel::where('id',$value['user_id'])->find();
            $goods_num = OrderGoodsModel::where('order_id',$value['id'])->value('goods_num');
            $value['avatar'] = $user['avatar'];
            $value['nickname'] = $user['nickname'];
            $value['goods_num'] = $goods_num;
        }
        $this->success('获取商品详情成功！',$order,200);
    }

    /**
     * 获取接龙商品id
     */
    public function getGoodsJielongId(){
        $jielong_goods_id = GoodsConfigModel::where(['key'=> 'jielong'])->value('goods_id');
        $this->success('获取接龙商品id成功！',$jielong_goods_id,200);
    }


    /**
     * 获取接龙商品详情
     */
    public function getGoodsJielongDetail(){
        $goods_id = GoodsConfigModel::where(['key'=> 'jielong'])->value('goods_id');
        $jielong_begin_time = GoodsConfigModel::where(['key'=> 'jielong_begin_time'])->value('goods_id');
        $jielong_end_time = GoodsConfigModel::where(['key'=> 'jielong_end_time'])->value('goods_id');
        $goods = GoodsModel::where('id',$goods_id)->find();
        $goods['images'] = explode('|',$goods['images']);
        $goods['intro'] = explode('|',$goods['intro']);
        //找到订单
        $order_ids = OrderGoodsModel::where('goods_id',$goods_id)->field('group_concat(order_id) as order_ids')->find()['order_ids'];
        $order = OrderModel::where('id','in',$order_ids)
            ->order('id desc')
            ->field('user_id,id,create_time')
            ->select();
        foreach ($order as $key=>&$value){
            $user = UserModel::where('id',$value['user_id'])->find();
            $goods_num = OrderGoodsModel::where('order_id',$value['id'])->value('goods_num');
            $value['avatar'] = $user['avatar'];
            $value['nickname'] = $user['nickname'];
            $value['goods_num'] = $goods_num;
        }
        $goods['order_count'] = OrderModel::where('id','in',$order_ids)->field('id')->count();
        $goods['order'] = $order;
        if($goods['type'] == SPEC_GOODS){
            $goods['market_price'] = GoodsProductModel::where('goods_id',$goods_id)->order('market_price asc')->find()['market_price'];//取最低
            $goods['price'] = GoodsProductModel::where('goods_id',$goods_id)->order('price asc')->find()['price'];//取最低
            $goods['stock'] = GoodsProductModel::where('goods_id',$goods_id)->order('stock asc')->find()['stock'];//取最低

            $goods['product'] = GoodsProductModel::where('goods_id',$goods_id)->select();
            //多规格与单规格
            $goods_specs = array();
            $current_specs = array();
            $goods_specs1 = array();
            $current_specs1 = array();
            $specs = GoodsSpecsModel::where('goods_id',$goods_id)->select()->toArray();
            foreach ($specs as $k=>$v) {
                $goods_specs[$v['spec_name']][]=$v;
                $current_specs[$v['spec_name']][]=$v;
            }

            foreach ($goods_specs as $k1=>$v1) {
                $goods_specs1[$k1]['name'] = $k1;
                $goods_specs1[$k1]['child'] = $v1;
            }
            foreach ($current_specs as $k2=>$v2) {
                $current_specs1[$k2]['name'] = $k2;
                $current_specs1[$k2]['value'] = '';

            }
            $goods['goods_specs'] = array_values($goods_specs1);
            $goods['current_specs'] = array_values($current_specs1);
            if(!empty($specs)){
                $goods['has_specs'] = true;
            }
        }else{
            $goods['product'] = [];
            $goods['goods_specs'] = [];
            $goods['current_specs'] = [];
            $goods['has_specs'] = false;
        }
        $goods['jielong_type'] = 'ok';
        if (!empty($jielong_begin_time)&&!empty($jielong_end_time)) {
//            $startTime = strtotime($goods['start_time']);
            $endTime = strtotime($jielong_end_time);
            $countDown = $endTime - time();
            if ($countDown <= 0) {
                $countDown = 0;
            }
            //剩余倒计时：毫秒时间戳
            $goods['countDown'] = $countDown * 1000;
            $goods['jielong_begin_time'] = $jielong_begin_time;
            $goods['jielong_end_time'] = $jielong_end_time;
            $goods['jielong_end_time'] = $jielong_end_time;
            if(strtotime($jielong_begin_time)>time()){
                $goods['jielong_type'] = 'not_begin';
            }
            if(strtotime($jielong_end_time)<time()){
                $goods['jielong_type'] = 'is_end';
            }
        }else{
            $goods['jielong_type'] = 'not_begin';
        }
        if(!empty($goods)){
            $this->success('获取接龙商品详情成功！',$goods,200);
        }else{
            $this->error('获取接龙商品详情失败！','',400);
        }
    }

}
